package com.sto.stoapp.activity.scanner.inquiry;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.StoDataBaseOperation;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.thread.BaseThread;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHomeActivity;

public class ResultInfoShowActivity extends StoHomeActivity {

	EnumTableNames tableEnum;
	String start, end;
	List<String> datas;
	ListView listView;
	private ArrayAdapter<String> adapter;
	RadioGroup state_radio;
	InquiryThread inquiryThread;
	boolean isAdimin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.send_state_lisview_show);

		init();
		initListener();
	}

	private void initListener() {
		state_radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.all:
					inquiryNotify(null);
					break;
				case R.id.send:
					inquiryNotify("已");
					break;
				case R.id.unsend:
					inquiryNotify("未");
					break;
				default:
					break;
				}

			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String text = adapter.getItem(arg2);
				String code = text.split(" ")[0];
				new InfoShowThread(ResultInfoShowActivity.this, code).start();
			}
		});

	}

	/**
	 * 显示详情
	 * 
	 * @param details
	 */
	@SuppressLint("ResourceAsColor")
	private void showDetails(String[] details) {
		if (details == null)
			Helper.showToast(this, "查询不到详情");
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		ListView detailsV = new ListView(this);
		detailsV.setBackgroundColor(R.color.white);
		ArrayAdapter<String> detailAdapter = new ArrayAdapter<String>(this,
				R.layout.details_item, R.id.details_item, details);
		detailsV.setAdapter(detailAdapter);
		detailsV.setBackgroundColor(R.color.white);
		builder.setView(detailsV);
		builder.create().show();
	}

	private synchronized void inquiryNotify(String state) {
		datas.clear();
		inquiryThread = new InquiryThread(this);
		inquiryThread.setState(state);
		inquiryThread.start();

	}

	private void init() {
		Intent intent = getIntent();
		start = intent.getStringExtra("start");
		end = intent.getStringExtra("end");

		tableEnum = (EnumTableNames) intent.getSerializableExtra("table_name");
		isAdimin = intent.getBooleanExtra("isAdimin", true);
		String action = intent.getStringExtra(HomeBroadAction.BROAD_TYPE);
		this.setTitleBottom(
				"扫描类型："
						+ getString(Integer.parseInt(tableEnum
								.getScanTypeName())), action);

		state_radio = (RadioGroup) findViewById(R.id.state_radio);
		listView = (ListView) findViewById(R.id.data_listview);
		datas = new ArrayList<String>();

		adapter = new ArrayAdapter<String>(this,
				R.layout.time_inquire_info_item, R.id.time_inquire_info_text,
				datas);

		listView.setAdapter(adapter);
		inquiryNotify(null);
	}

	class InquiryThread extends BaseThread {
		Context context;
		String state;

		public InquiryThread(Context context) {
			super(context);
			this.context = context;
			showDialog();
		}

		public void setState(String state) {
			this.state = state;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			String jobNum;
			if (isAdimin) {
				jobNum = null;
			} else {
				jobNum = StoFilePreference.getInstance(context).getUserCode();
			}

			datas.addAll(StoDataBaseOperation.getInstance(context)
					.getCodesInfoByJobAndState(tableEnum, jobNum, state, start,
							end));
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					adapter.notifyDataSetChanged();
				}

			});
			cancelDialog();
		}
	}

	class InfoShowThread extends BaseThread {
		String code;
		Context context;

		public InfoShowThread(Context context, String code) {
			super(context);
			this.code = code;
			this.context = context;
			showDialog();
		}

		@Override
		public void run() {
			super.run();
			final String[] bus = InquiryQueryTable.getInstance(context)
					.getCodeInquiryDetail(tableEnum.getTableName(), code,
							tableEnum.ordinal());
			cancelDialog();
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					showDetails(bus);

				}
			});
		}
	}
}
